//
//  BaseVC.swift
//  SearchHeader
//
//  Created by 万孟超 on 2023/10/16.
//

import Cocoa

class BaseVC: NSViewController {
    
    private lazy var sameWindow: NSWindowController = {
        let contentWindow = NSWindow(contentRect: view.bounds, styleMask: [.titled, .closable, .miniaturizable, .resizable], backing: .buffered, defer: false)
        contentWindow.titlebarAppearsTransparent = true
        contentWindow.titleVisibility = .hidden
        contentWindow.isMovableByWindowBackground = true
        contentWindow.center()
        return NSWindowController(window: contentWindow)
    }()
    
    private var viewSize: NSSize = NSMakeSize(800, 650)
    
    private var isFirtLoad: Bool = true
    
    convenience init(nibName: NSNib.Name? = nil, bundle: Bundle? = nil, size: NSSize) {
        self.init(nibName: nibName, bundle: bundle)
        self.viewSize = size
    }
    
    convenience init() {
        self.init(size: NSMakeSize(800, 650))
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.clear.cgColor
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        // Do view setup here.
        if isFirtLoad {
            installSubViews()
            installViewsContains()
            isFirtLoad = false
        }
    }
    
    func installSubViews() { }
    
    func installViewsContains() { }
    
    override func loadView() {
        let visualEffectView = NSVisualEffectView()
        visualEffectView.state = .active
        visualEffectView.material = .hudWindow // 根据系统外观选择合适的材质
        visualEffectView.wantsLayer = true
        visualEffectView.layer?.backgroundColor = NSColor.clear.cgColor
        visualEffectView.frame = NSRect(origin: .zero, size: viewSize)
        view = visualEffectView
    }
    
    func presentNewWindow(_ controller: NSViewController) {
        let contentWindow = NSWindow(contentRect: view.bounds, styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], backing: .buffered, defer: false)
        contentWindow.titlebarAppearsTransparent = true
        contentWindow.titleVisibility = .hidden
        contentWindow.isMovableByWindowBackground = true
        contentWindow.contentViewController = controller
        contentWindow.center()
        let windowController = NSWindowController(window: contentWindow)
        windowController.showWindow(self)
    }
    
    func presentSameWindow(_ controller: NSViewController) {
        sameWindow.close()
        sameWindow.contentViewController = controller
        sameWindow.showWindow(self)
    }
}
